<?php

class SearchController {
	
    public static function searchCondition($text) {
    	
		$resultEvent = array();
		$resultCount = 0;
		
		//CONDICTION EVENT NAME
		$result = DataBaseController::execute("SELECT * FROM ".Constans::$EVENT." WHERE name LIKE '%$text%'");	
		while($data=mysql_fetch_assoc($result)){
			$eventId = 	$data['event_id'];
			if(!SearchController::cheackArray($resultEvent, $eventId)){
				$resultEvent[$resultCount] = $eventId;
				$resultCount++;
			}
		}
		//CONDICTION TAG
		$result = DataBaseController::execute("SELECT * FROM ".Constans::$TAG." WHERE name LIKE '%$text%'");	
		while($data=mysql_fetch_assoc($result)){
			$tagId = $data['tag_id'];
			$tagResult = DataBaseController::execute("SELECT * FROM ".Constans::$TAGMAPPING." WHERE tag_id='$tagId'");
			while($tagData=mysql_fetch_assoc($tagResult)){
				$eventId = 	$tagData['event_id'];
				if(!SearchController::cheackArray($resultEvent, $eventId)){
					$resultEvent[$resultCount] = $eventId;
					$resultCount++;
				}
			}
		}	
		//CONDICTION CATEGORY
		$result = DataBaseController::execute("SELECT * FROM ".Constans::$CATEGORY." WHERE name LIKE '%$text%'");	
		while($data=mysql_fetch_assoc($result)){
			$categoryId = $data['category_id'];
			//$level = $data['level'];
			//$parentcategory = $data['parentcategory'];
			
			$categoryResult = DataBaseController::execute("SELECT * FROM ".Constans::$CATEGORYMAPPING." WHERE category_id='$categoryId'");
			while($categoryData=mysql_fetch_assoc($categoryResult)){
				$eventId = $categoryData['event_id'];
				if(!SearchController::cheackArray($resultEvent, $eventId)){
					$resultEvent[$resultCount] = $eventId;
					$resultCount++;
				}
			}
			
			$parentcategoryResult = DataBaseController::execute("SELECT * FROM ".Constans::$CATEGORY." WHERE parentcategory='$categoryId'");
			while($parentcategoryData=mysql_fetch_assoc($parentcategoryResult)){
				$categoryId = $parentcategoryData['category_id'];

				$categoryResult = DataBaseController::execute("SELECT * FROM ".Constans::$CATEGORYMAPPING." WHERE category_id='$categoryId'");
			       while($categoryData=mysql_fetch_assoc($categoryResult)){
				   $eventId = $categoryData['event_id'];
				   if(!SearchController::cheackArray($resultEvent, $eventId)){
   					  $resultEvent[$resultCount] = $eventId;
	  				  $resultCount++;
		   		   }
			    }				
			}
		}	
		
		//CONDICTION EVENTLOCATION 
		//name
		$result = DataBaseController::execute("SELECT * FROM ".Constans::$EVENTLOCATION." WHERE name LIKE '%$text%'");
		while($data=mysql_fetch_assoc($result)){
			$eventlocationId = $data['eventlocation_id'];
			$eventResult = DataBaseController::execute("SELECT * FROM ".Constans::$EVENT." WHERE eventlocation_id='$eventlocationId'");
			while($eventData=mysql_fetch_assoc($eventResult)){
				$eventId = $eventData['event_id'];
				if(!SearchController::cheackArray($resultEvent, $eventId)){
					$resultEvent[$resultCount] = $eventId;
					$resultCount++;
				}
			}
		}
		//location	
		$result = DataBaseController::execute("SELECT * FROM ".Constans::$EVENTLOCATION." WHERE location LIKE '%$text%'");
		while($data=mysql_fetch_assoc($result)){
			$placeId = $data['eventlocation_id'];
			$eventResult = DataBaseController::execute("SELECT * FROM ".Constans::$EVENT." WHERE eventlocation_id='$eventlocationId'");
			while($eventData=mysql_fetch_assoc($eventResult)){
				$eventId = $eventData['event_id'];
				if(!SearchController::cheackArray($resultEvent, $eventId)){
					$resultEvent[$resultCount] = $eventId;
					$resultCount++;
				}
			}
		}	
		
		return $resultEvent;
    }
    
	public static function cheackArray($array, $item) {
		if (count($array)==0){
			return false;
		}
		foreach ($array as $element) {
			if($element==$item){
				return true;
			}
		}
		return false;
	}    
	
	public static function valueCondition($resultEvent, $valueMin, $valueMax) {
		if($valueMin==0 && $valueMax==0){
			return $resultEvent;
		}
		$newResultEvent = array();
		$count = 0;
		foreach ($resultEvent as $eventId) {
			if($valueMin!=0 && $valueMax!=0){
				$qs = "SELECT * FROM ".Constans::$EVENT." WHERE event_id='$eventId' and value>='$valueMin' and value<='$valueMax'";
			}
			if($valueMin==0 && $valueMax!=0){
				$qs = "SELECT * FROM ".Constans::$EVENT." WHERE event_id='$eventId' and value<='$valueMax'";
			}	
			if($valueMin!=0 && $valueMax==0){
				$qs = "SELECT * FROM ".Constans::$EVENT." WHERE event_id='$eventId' and value>='$valueMin'";
			}						
			$r = DataBaseController::execute($qs);
			if(mysql_numrows($r)>0){	
				$newResultEvent[$count] = $eventId;
				$count++;	
			}			
		}		
		return $newResultEvent;
	}
}

?>